package com.epoint.test;

import com.epoint.com.epoint.enums.InspectionState;
import com.epoint.domain.Inspection;
import com.epoint.mapper.InspectionMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.*;

public class Demo {

    /**
     * 最简单，纯净版
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 获取重点对象， sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        try {
            Inspection in = session.selectOne("com.epoint.inspection.selectList", 1);

            System.out.println(in);

        }finally {
            session.close();
        }
    }

    /**
     * 面向接口开发
     * @throws IOException
     */
    @Test
    public void test02() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 获取重点对象， sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        try {
            InspectionMapper mapper = session.getMapper(InspectionMapper.class);

            Inspection in = mapper.getInspectionById("1");
            System.out.println(in);

            // mybatis动态代理，代理类这个接口。mapper就是proxy帮忙生成的代理对象
            // org.apache.ibatis.binding.MapperProxy@7113b13f
            System.out.println(mapper);

        }finally {
            session.close();
        }
    }

    /**
     * insert
     */
    @Test
    public void test03() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 获取重点对象， sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // openSession的参数：true-自动提交事务， 否则-手动提交
        SqlSession session = sqlSessionFactory.openSession(true);

        try {
            InspectionMapper mapper = session.getMapper(InspectionMapper.class);
            Inspection inspection = new Inspection();
            inspection.setRowguid(UUID.randomUUID().toString());
            inspection.setProjectguid("adminproject");
            inspection.setName("几款好");
            inspection.setCreateDate(new Date());

            int i = mapper.addInspection(inspection);

            System.out.println(i);
        }finally {
            session.close();
        }
    }

    /**
     * update
     */
    @Test
    public void test04() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 获取重点对象， sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // openSession的参数：true-自动提交事务， 否则-手动提交
        SqlSession session = sqlSessionFactory.openSession(true);

        try {
            InspectionMapper mapper = session.getMapper(InspectionMapper.class);
            Inspection inspection = new Inspection();
            inspection.setRowguid("1");
            inspection.setProjectguid("测试update");
            inspection.setName("测试update");
            inspection.setCreateDate(new Date());

            int i = mapper.updateInspection(inspection);

            System.out.println(i);
        }finally {
            session.close();
        }
    }

    /**
     * delete
     */
    @Test
    public void test05() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 获取重点对象， sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // openSession的参数：true-自动提交事务， 否则-手动提交
        SqlSession session = sqlSessionFactory.openSession(true);

        try {
            InspectionMapper mapper = session.getMapper(InspectionMapper.class);
            int i = mapper.deleteInspection("1");

            System.out.println(i);
        }finally {
            session.close();
        }
    }

    /**
     * 参数的各种演示
     */
    @Test
    public void test06() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 获取重点对象， sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // openSession的参数：true-自动提交事务， 否则-手动提交
        SqlSession session = sqlSessionFactory.openSession(true);

        try {
            InspectionMapper mapper = session.getMapper(InspectionMapper.class);
            Inspection inspectionByIdAndProjectguid = null;

//            inspectionByIdAndProjectguid = mapper.getInspectionByIdAndProjectguid("1", "1");
//            inspectionByIdAndProjectguid = mapper.getInspectionByIdAndProjectguidAndAnno("1", "1");

//            Map<String, Object> params = new HashMap<String, Object>();
//            params.put("rowguid", "1");
//            params.put("projectguid", "1");
//            inspectionByIdAndProjectguid = mapper.getInspectionByMap(params);

//            Inspection inspection = new Inspection();
//            inspection.setRowguid("1");
//            inspection.setProjectguid("1");
//            inspectionByIdAndProjectguid = mapper.getInspectionByPoJo(inspection);

//            List<Inspection> allInspection = mapper.getAllInspection();
//            System.out.println(allInspection);

//            Map<String, Object> inspection2MapById = mapper.getInspection2MapById("1");
//            System.out.println(inspection2MapById);

//            Map<String, Inspection> inspection2MapAndIdById = mapper.getInspection2MapAndIdById("1");
//            System.out.println(inspection2MapAndIdById);
//            List<Map<String, Object>> inspection2MapListAndIdById = mapper.getInspection2MapListAndIdById("2");
//            for(Map<String, Object> in : inspection2MapListAndIdById){
//                System.out.println(in.getClass().getName());
//                System.out.println(in);
//            }

//            Inspection inspectionAndRecordListById = mapper.getInspectionAndRecordListById("2");
//            System.out.println(inspectionAndRecordListById);

//            List<Inspection> inspectionAndRecordList = mapper.getInspectionAndRecordList();
//            System.out.println(inspectionAndRecordList);

//            Map<String, Object> params = new HashMap<String, Object>();
//            params.put("rowguid", "1");
//            params.put("projectguid", "adminproject");
//            params.put("createdate", new Date());
//            List<Inspection> inspectionListByDynamicSql = mapper.getInspectionListByDynamicSql(params);
//            System.out.println(inspectionListByDynamicSql);

            List<Inspection> inspectionListByDynamicSql2 = mapper.getInspectionListByDynamicSql2(Arrays.asList("1", "2"));
            System.out.println(inspectionListByDynamicSql2);
        }finally {
            session.close();
        }
    }

    /**
     * 分页插件
     * @throws IOException
     */
    @Test
    public void test07() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 获取重点对象， sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // openSession的参数：true-自动提交事务， 否则-手动提交
        SqlSession session = sqlSessionFactory.openSession(true);

        try {
            InspectionMapper mapper = session.getMapper(InspectionMapper.class);
            Page<Object> page = PageHelper.startPage(5, 1);

            List<Inspection> allInspection = mapper.getAllInspection();
            System.out.println(allInspection);
        }finally {
            session.close();
        }
    }

    /**
     * 批量插入
     * @throws IOException
     */
    @Test
    public void test08() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 获取重点对象， sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // ExecutorType.BATCH：批量模式
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);

        try {
            long start = System.currentTimeMillis();

            InspectionMapper mapper = session.getMapper(InspectionMapper.class);

            for(int i = 0; i < 10000; i++){
                Inspection inspection = new Inspection();
                inspection.setRowguid(UUID.randomUUID().toString());
                inspection.setProjectguid("adminproject");
                inspection.setName("纪宽豪");
                inspection.setCreateDate(new Date());

                mapper.addInspection(inspection);
            }

            // 提交事务
            // 其实这个例子有2个知识点，一个是 for循环结束后 最后commit提交事务，这样好一点 不必每次插入都commit提交事务
            // 第二个就是mybatis的批处理，能更快
            session.commit();

            System.out.println(System.currentTimeMillis() - start);

            /**
             * 整合Spring，需要注册2个sqlSessionFactory 一个是简单版的，一个批量版的（因为批量版比较费性能）
             *
             *  配置一个可以进行批量执行的sqlSession
             * 	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
             * 		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
             * 		<constructor-arg name="executorType" value="BATCH"></constructor-arg>
             * 	</bean>
             *
             *
             */
        }finally {
            session.close();
        }
    }

    @Test
    public void testEnum(){
        InspectionState finish = InspectionState.FINISH;
        System.out.println(finish.getText());
        System.out.println(finish.getValue());

    }


}
